<template>
  <teamListCard :team-list="teamList" v-if="teamList.length > 0&&teamList!==null">
    <template v-slot="slotProps">
      <van-button
        size="small"
        plain
        type="default"
        @click="exitTeam(slotProps.id)"
        >退出</van-button
      >
    </template>
  </teamListCard>
  <van-empty image="search" description="还没有加入队伍" v-else />
  <van-notify id="van-notify" />
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import teamListCard from '../components/teamListCard.vue'
import { hasJoinTeam } from '../api/team/team'
import useStore from '../store'
import { quitTeam } from '../api/team/team'
const teamList = ref([])
const getJoinTeamList = async () => {
  const res = await hasJoinTeam()
  console.log(res)
  teamList.value = res.data
}
//退出队伍
const exitTeam = (teamId) => {
  uni.showModal({
    title: '提示',
    content: '确认退出队伍？',
    success: async (res) => {
      if (res.confirm) {
        console.log('用户点击确定')
        const res = await quitTeam({ teamId })

        if (res.code === 0) {
          uni.showToast({
            title: '成功',
            duration: 2000,
            icon: 'success',
            success: () => {
              getJoinTeamList()
            },
          })
        } else {
          uni.showToast({
            title: res.description,
            duration: 2000,
            icon: 'fail',
          })
        }
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    },
  })
}
useStore().setting().btnStatusChange(1)
onMounted(() => {
  getJoinTeamList()
})
onUnmounted(() => {
  useStore().setting().btnStatusChange(0)
})
</script>
